package com.qiang.testspecialview;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

public class RegularHexagonLayout extends ViewGroup {
    private static final String TAG = "RegularHexagonLayout";
    /**
     * 布局宽度由RADIU_COUNT个正六边形半径加上一定数量PADDING组成.
     * 这里仅列举常用数量:
     * RADIU_COUNT=5, Padding数量为2, 总共7个正六边形;
     * RADIU_COUNT=8, Padding数量为4, 总共19个正六边形;
     */
    private static final int RADIU_COUNT = 5;
//    private static final int RADIU_COUNT = 8;

    /**相邻2正六边形的间距(暂未使用,目前间距由背景图片控制,如果要将间距变成可配置,
     * 则需在onMesure()中计算childRadius,以及computerPoint()中计算所有正六边形中心点坐标时考虑上.)*/
    private static final int PADDING = 0;

    /**正六边形半径*/
    private int childRadius;
    /**正六边形宽度*/
    private int childWidth;
    /**正六边形高度*/
    private int childHeight;
    /**正六边形数量*/
    private int mChildCount;
    /**父容器中心点*/
    private int centerX ,centerY;

    private ArrayList<CircleCenteter> centers = new ArrayList<RegularHexagonLayout.CircleCenteter>(7);

    public RegularHexagonLayout(Context context) {
        super(context);
    }

    public RegularHexagonLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public RegularHexagonLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int wMode = MeasureSpec.getMode(widthMeasureSpec);
        int wSize = MeasureSpec.getSize(widthMeasureSpec);

        int hMode = MeasureSpec.getMode(heightMeasureSpec);
        int hSize = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(wSize, hSize);

        centerX = wSize / 2;
        centerY = hSize / 2;
        childRadius = wSize / RADIU_COUNT;  //不考虑相邻2控件的间距.
        childWidth = childRadius * 2;
        childHeight = (int)(childRadius * Math.sqrt(3) / 2)*2;
        final int count = getChildCount();
        for (int index = 0; index < count; index++) {
            View child = getChildAt(index);
            // measure
            child.measure(childWidth, childHeight);
        }
        if (mChildCount != count) {
            mChildCount = count;
        }
//        if (mChildCount > centers.size()) {
            computerPoint(centerX, centerY, childHeight);
//        }
        Log.i(TAG, "onMeasure()--childWidth="+childWidth+",childHeight="+childHeight);
        Log.i(TAG, "onMeasure()--wMode=" + wMode + ",wSize=" + wSize + ",hMode=" + hMode+ ",hSize=" + hSize);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        final int count = getChildCount();
        int childLeft, childTop;
        for (int i = 0; i < count; i++) {
            View child = getChildAt(i);
            childLeft = (int)(centers.get(i).x-childRadius);
            childTop =  (int)(centers.get(i).y-childHeight/2);
            child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
        }
        Log.i(TAG, "onLayout()--changed=" + changed + ",left=" + left + ",top=" + top + ",right="
                + right + ",bottom=" + bottom + ",count=" + count);
    }


    /**
     * 根据正六边形的索引计算它位于第几层(圈).
     * @param position 正六边形的索引
     * @return
     */
    public int getCircleIndex(int position) {
        int circleIndex = 0;
        while (position > (3*circleIndex*circleIndex + 3*circleIndex)) {
            circleIndex ++;
        }
        return circleIndex;
    }

    /**
     * 计算每层有多少个正六边形.
     * @param circleIndex  第几层(第几圈)
     * @return
     */
    public int getCircleCount(int circleIndex) {
        if (circleIndex == 0) {
            return 1;
        }
        return circleIndex*6;
    }

    /**
     * 根据父容器中心点及正六边形高度计算所有正六边形的中心点坐标.
     * @param a
     * @param b
     * @param h
     */
    private void computerPoint(double a, double b, double h) {
        double sqrt3 = Math.sqrt(3);

        //c01表示正中心的正六边形;
        //cmn,m表示第m圈, n表示该圈第n个正六边形.
        CircleCenteter c01 = new CircleCenteter(a, b);
        //第一圈正六边形:
        CircleCenteter c11 = new CircleCenteter(a, b-h);
        CircleCenteter c12 = new CircleCenteter(a + sqrt3*h/2, b - h/2);
        CircleCenteter c13 = new CircleCenteter(a + sqrt3*h/2, b + h/2);
        CircleCenteter c14 = new CircleCenteter(a, b + h);
        CircleCenteter c15 = new CircleCenteter(a - sqrt3*h/2, b + h/2);
        CircleCenteter c16 = new CircleCenteter(a - sqrt3*h/2, b - h/2);

        //第二圈正六边形
//        CircleCenteter c21 = new CircleCenteter(a, b-2*h);
//        CircleCenteter c22 = new CircleCenteter(a + sqrt3*h/2, b-3*h/2);
//        CircleCenteter c23 = new CircleCenteter(a + sqrt3*h, b - h);
//        CircleCenteter c24 = new CircleCenteter(a + sqrt3*h, b);
//        CircleCenteter c25 = new CircleCenteter(a + sqrt3*h, b + h);
//        CircleCenteter c26 = new CircleCenteter(a + sqrt3*h/2, b + 3*h/2);
//        CircleCenteter c27 = new CircleCenteter(a, b + 2*h);
//        CircleCenteter c28 = new CircleCenteter(a - sqrt3*h/2, b + 3*h/2);
//        CircleCenteter c29 = new CircleCenteter(a - sqrt3*h, b + h);
//        CircleCenteter c210 = new CircleCenteter(a - sqrt3*h, b);
//        CircleCenteter c211 = new CircleCenteter(a - sqrt3*h, b - h);
//        CircleCenteter c212 = new CircleCenteter(a - sqrt3*h/2, b-3*h/2);

        centers.clear();
        centers.add(c01);

        centers.add(c11);
        centers.add(c12);
        centers.add(c13);
        centers.add(c14);
        centers.add(c15);
        centers.add(c16);

//        centers.add(c21);
//        centers.add(c22);
//        centers.add(c23);
//        centers.add(c24);
//        centers.add(c25);
//        centers.add(c26);
//        centers.add(c27);
//        centers.add(c28);
//        centers.add(c29);
//        centers.add(c210);
//        centers.add(c211);
//        centers.add(c212);

    }



    class CircleCenteter {
        double x ,y;
        public CircleCenteter(double x, double y){
            this.x = x;
            this.y = y;
        }
    }

}
